package com.bbs.toutiao.utils;


import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;


/**
 * 描述: 阿里云短信验证
 * 作者： zhangBin
 * 时间： 2023/10/8 20:19
 * 版本： 1.0
 */


@Component
public class SMSUtil {
    final static String accessKeyId = "LTAI5tED5Myvo15kLywaf2ub";
    final static String accessKeySecret = "4P6ZoVhVsqhIaU7BQejG7x5o1TEEmB";
    final static String signName = "ABC的网站";
    final static String templateCode = "SMS_463626964";

    /**
     * 阿里云发送手机短信验证码
     * phone ：手机号
     * @param phone captcha
     * @return
     */
    public void sendSMS(String phone, String captcha) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        request.setSignName(signName);
        //手机号
        request.putQueryParameter("PhoneNumbers", phone);
        //模板code
        request.putQueryParameter("TemplateCode", templateCode);
        //验证码  使用json格式
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + captcha + "\"}");

        try {
            SendSmsResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
            // 打印您需要的返回值，此处打印的是此次请求的 RequestId
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            // 打印错误码
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }

    }
}
